using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ZedGraph;
using ZedGraph.Web;
using System.Drawing;
using System.Collections.Generic;


public class Grafieken
{

    private Databank db;
    private string Gebruiker;
    private String Richting;

	public Grafieken(String GebruikerID)
	{
        db = Databank.DatabankInstantie;
        this.Gebruiker = GebruikerID;
	}


    public void HorizontaleBalken(ZedGraphWeb webObject, Graphics g, MasterPane masterPane, String Titel, String Xas, 
        String Yas, AgendaItem[,] klasgegevens, int RichtingID)
    {
        GraphPane myPane = masterPane[0];

        myPane.Title.Text = Titel;
        myPane.YAxis.Title.Text = Yas;
        myPane.XAxis.Title.Text = Xas;

        List<Vak> Vakken = db.GetVakkenRichting(RichtingID);

        string[] labels = { "Panther", "Lion", "Cheetah", "Cougar", "Tiger", "Leopard" };
        double[] x = { 100, 115, 75, 22, 98, 40 };

        BarItem myCurve = myPane.AddBar("Here", x, null, Color.Red);
        myCurve.Bar.Fill = new Fill(Color.Red, Color.White, Color.Red, 90f);

        myPane.YAxis.MajorTic.IsBetweenLabels = true;

        myPane.YAxis.Scale.TextLabels = labels;
        myPane.YAxis.Type = AxisType.Text;

        myPane.BarSettings.Type = BarType.Stack;

        myPane.BarSettings.Base = BarBase.Y;

        myPane.Chart.Fill = new Fill(Color.White,
            Color.FromArgb(255, 255, 166), 45.0F);

        masterPane.AxisChange(g);
    }


    public void VergelijkingIkkeKlas(ZedGraphWeb webObject, Graphics g, MasterPane masterPane, String Titel, String Xas,
        String Yas, AgendaItem[,] klasgegevens, int RichtingID, TextBox leg)
    {

        String Legende = "";

        GraphPane myPane = masterPane[0];

        myPane.Title.Text = Titel;
        myPane.YAxis.Title.Text = Xas;
        myPane.XAxis.Title.Text = Yas;

        PointPairList list = new PointPairList();
        PointPairList list2 = new PointPairList();

        List<Vak> Vakken = db.GetVakkenRichting(RichtingID);
        string[] labels = new string[Vakken.Count ];
        for (int i = 0; i < Vakken.Count; i++)
        {
            labels[i] = i.ToString(); 
            Legende += "\n" + i.ToString() + " "  + Vakken[i].Naam;
        }

        string AgendaID;
        AgendaID = db.GetAgendaID(Gebruiker);

        for (int i = 0; i < Vakken.Count; i++)
        {
            double y =db.GetGemiddeldStudieTijdPerVak(Vakken[i].Id.ToString());  
 
           double x = (double)i;
           list.Add(x, (y/60));
           double y2 = db.GetStudieTijdPerVak(Vakken[i].Id.ToString(), AgendaID);
           list2.Add(x, y2);
        }

        myPane.XAxis.Scale.TextLabels = labels;
        myPane.XAxis.Type = AxisType.Text;

        BarItem myCurve = myPane.AddBar("Klas", list, Color.Blue);
        BarItem myCurve2 = myPane.AddBar("Jij", list2, Color.Red);

        myPane.Chart.Fill = new Fill(Color.White,
            Color.FromArgb(255, 255, 166), 45.0F);

        masterPane.AxisChange(g);

        myPane.YAxis.Scale.Max += myPane.YAxis.Scale.MajorStep;

        BarItem.CreateBarLabels(myPane, false, "f0");
        leg.Text = Legende;
    }


    public void VergelijkingIkkeKlasLijnen(ZedGraphWeb webObject, Graphics g, MasterPane masterPane, String Titel, String Xas,
        String Yas, AgendaItem[,] klasgegevens, int RichtingID, TextBox leg)
    {
           GraphPane myPane = masterPane[0];
           String Legende = "";
           myPane.Title.Text = Titel;
           myPane.XAxis.Title.Text = Xas;
           myPane.YAxis.Title.Text = Yas;

           PointPairList list1 = new PointPairList();
           PointPairList list2 = new PointPairList();

           List<Vak> Vakken = db.GetVakkenRichting(RichtingID);
           string[] labels = new string[Vakken.Count];
           for (int i = 0; i < Vakken.Count; i++)
           {
               labels[i] = i.ToString();
               Legende += "\n" + ((int)(i+1)).ToString() + " " + Vakken[i].Naam;
           }

           string AgendaID;
           AgendaID = db.GetAgendaID(Gebruiker);

           for (int i = 0; i < Vakken.Count; i++)
           {
               double y = db.GetGemiddeldStudieTijdPerVak(Vakken[i].Id.ToString());

               double x = (double)i+1;
               list1.Add(x, (y / 60));
               double y2 = db.GetStudieTijdPerVak(Vakken[i].Id.ToString(), AgendaID);
               list2.Add(x, y2);
           }


           LineItem myCurve = myPane.AddCurve( "Klas",list1, Color.Blue, SymbolType.Diamond );

           LineItem myCurve2 = myPane.AddCurve( "jij",list2, Color.Red, SymbolType.Circle );

           myPane.Title.FontSpec.FontColor = Color.Green;
   
           myPane.XAxis.MajorGrid.IsVisible = true;
           myPane.YAxis.MajorGrid.IsVisible = true;
           myPane.XAxis.MajorGrid.Color = Color.LightGray;
           myPane.YAxis.MajorGrid.Color = Color.LightGray; 
           
           myPane.Legend.Position = ZedGraph.LegendPos.Bottom;

           myCurve.Line.Width = 2.0F;
           myCurve2.Line.Width = 2.0F;

           myCurve.Line.Fill = new Fill(Color.White, Color.Blue, 45F);
           myCurve2.Line.Fill = new Fill(Color.White, Color.Red, 45F);

           myCurve.Symbol.Size = 8.0F;
           myCurve2.Symbol.Size = 8.0F;
           myCurve.Symbol.Fill = new Fill(Color.White);
           myCurve2.Symbol.Fill = new Fill(Color.White);

           myPane.Chart.Fill = new Fill(Color.White,
              Color.FromArgb(255, 255, 210), -45F);

           masterPane.AxisChange();
           leg.Text = Legende;

    }
   

    public void IkkePie(ZedGraphWeb webObject, Graphics g, MasterPane masterPane, String Titel, AgendaItem[,] klasgegevens)
    {
        GraphPane myPane = masterPane[0];

        myPane.Title.Text = Titel;
        myPane.Title.FontSpec.IsItalic = true;
        myPane.Title.FontSpec.Size = 24f;
        myPane.Title.FontSpec.Family = "Times New Roman";

        myPane.Fill = new Fill(Color.White, Color.WhiteSmoke, 45.0f);

        myPane.Chart.Fill.Type = FillType.None;

        myPane.Legend.Position = LegendPos.Float;
        myPane.Legend.Location = new Location(0.95f, 0.15f, CoordType.PaneFraction,
                       AlignH.Right, AlignV.Top);
        myPane.Legend.FontSpec.Size = 10f;
        myPane.Legend.IsHStack = false;

        List<Vak> vakken = db.GetVakkenRichtingStudent(Gebruiker);
        PieItem[] segment = new PieItem[vakken.Count];

        string AgendaID;
        AgendaID = db.GetAgendaID(Gebruiker);
        Random rnd = new Random();
        
        for (int i = 0; i < vakken.Count; i++)
        {
            double tijd = db.GetStudieTijdPerVak(vakken[i].Id.ToString(), AgendaID);
           
            int rood = rnd.Next(255);
            int groen = rnd.Next(255);
            int blauw = rnd.Next(255);

            segment[i] = myPane.AddPieSlice(tijd, Color.FromArgb(rood, groen, blauw), Color.White, 45f, 0, vakken[i].Code.ToString() + " " + tijd);
        }
                                                       
        CurveList curves = myPane.CurveList;
        double total = 0;
        for (int x = 0; x < curves.Count; x++)
        {
            total += ((PieItem)curves[x]).Value;
        }

        TextObj text = new TextObj("Total uur gestudeerd\n"  + total.ToString(),
                       0.18F, 0.40F, CoordType.PaneFraction);
        text.Location.AlignH = AlignH.Center;
        text.Location.AlignV = AlignV.Bottom;
        text.FontSpec.Border.IsVisible = false;
        text.FontSpec.Fill = new Fill(Color.White, Color.FromArgb(255, 100, 100), 45F);
        text.FontSpec.StringAlignment = StringAlignment.Center;
        myPane.GraphObjList.Add(text);

        TextObj text2 = new TextObj(text);
        text2.FontSpec.Fill = new Fill(Color.Black);
        text2.Location.X += 0.008f;
        text2.Location.Y += 0.01f;
        myPane.GraphObjList.Add(text2);

       masterPane.AxisChange();
    }


    public void GlobaalGrafiek(ZedGraphWeb webObject, Graphics g, MasterPane masterPane, String Titel, String Richting,
        String Vakid, String Xas, String Yas,TextBox txtbox)
    {
         GraphPane myPane = masterPane[0];
         int stop = 0;
        myPane.Title.Text = Titel;
        myPane.YAxis.Title.Text = Yas;

        //Geef alle gebruikers uit de gegeven richting
        List<String> users = new List<String>();
        users = db.GetRichtingGebruikers(int.Parse(Richting));
        List<double> studietijd = new List<double>();
        string textb="";
   
        switch (int.Parse(Vakid))
        {
            case 0:
                myPane.XAxis.Title.Text = "vak";


                string agenda = db.GetAgendaIDRichting(Richting);
                List<Vak> vakken = new List<Vak>();
                vakken = db.GetVakkenRichting(int.Parse(Richting));
                int i = 1;
                foreach (Vak v in vakken)
                {
                    studietijd.Add(db.GetStudieTijdPerVak(v.Id.ToString()));
                    textb += "\n" + i.ToString() + " " + v.Naam;
                        i++;
                }

                stop = vakken.Count;
                break;
            default:
                //Zoek Voor elke gebruiker heoveel hij heeft gestudeerd voor vak X
                foreach (string gebruiker in users)
                {
                    studietijd.Add(db.GetStudieTijdPerStudentVoorVak(Vakid,gebruiker));
                }
                myPane.XAxis.Title.Text = Xas;
                stop = users.Count;
                break;
        }




        PointPairList list = new PointPairList();
        for ( int i = 0; i < stop; i++ )
        {
            double x = i + 1;

            double y = studietijd[i] ;
            list.Add( x, y );
        }

        myPane.Legend.IsVisible = false;

        LineItem curve = myPane.AddCurve( "label", list, Color.Red, SymbolType.Diamond);
        curve.Line.Width = 2.0F;
        curve.Line.IsAntiAlias = true;
        curve.Symbol.Fill = new Fill( Color.White );
        curve.Symbol.Size = 7;
    
        myPane.Chart.Fill = new Fill( Color.White, Color.FromArgb( 255, Color.ForestGreen ), 45.0F );

        const double offset = 1.0;

        for ( int i = 0; i < stop; i++ )
        {
            PointPair pt = curve.Points[i];
        
            TextObj text = new TextObj( pt.Y.ToString( "f2" ), pt.X, pt.Y + offset,
            CoordType.AxisXYScale, AlignH.Left, AlignV.Center );
            text.ZOrder = ZOrder.A_InFront;
            text.FontSpec.Border.IsVisible = false;
            text.FontSpec.Fill.IsVisible = false;
            text.FontSpec.Angle = 90;
            
            myPane.GraphObjList.Add( text );
         }

        myPane.YAxis.Scale.MaxGrace = 0.2;

        masterPane.AxisChange();

        txtbox.Text = textb;

    }


    public void Curve(ZedGraphWeb webObject, Graphics g, MasterPane masterPane)
    {
        GraphPane myPane = masterPane[0];

        myPane.Title.Text = "My Test Date Graph";
        myPane.XAxis.Title.Text = "Date";
        myPane.YAxis.Title.Text = "My Y Axis";

        PointPairList list = new PointPairList();
        PointPairList list2 = new PointPairList();
        for (int i = 0; i < 36; i++)
        {
            double x = new XDate(1995, i + 1, 1);
            double y = Math.Sin((double)i * Math.PI / 15.0);
            double y2 = 2 * y;

            list.Add(x, y);
            list2.Add(x, y2);
        }

        LineItem myCurve2 = myPane.AddCurve("My Curve 2", list, Color.Blue,
                                SymbolType.Circle);
        myCurve2.Line.Fill = new Fill(Color.White, Color.Red, 45F);
        myCurve2.Symbol.Fill = new Fill(Color.White);

        LineItem myCurve = myPane.AddCurve("My Curve",
            list2, Color.MediumVioletRed, SymbolType.Diamond);
        myCurve.Line.Fill = new Fill(Color.White, Color.Green);
        myCurve.Symbol.Fill = new Fill(Color.White);

        myPane.XAxis.Type = AxisType.Date;
        myPane.XAxis.CrossAuto = true;

        myPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45F);

        masterPane.AxisChange(g);
    }

    public void OverzichtKlassen(ZedGraphWeb webObject, Graphics g, MasterPane masterPane, String Titel, String Vakid,
        String Xas, String Yas,bool on ,DateTime starttime,DateTime stoptime)
    {
        GraphPane myPane = masterPane[0];

        myPane.Title.Text = Titel;
        myPane.XAxis.Title.Text = Xas;
        myPane.YAxis.Title.Text = Yas;

        List<double> data = new List<double>();
        switch (on)
        {
            case false :
            data = db.GetStudietijdVakGBStudent(Vakid);
                break;
            case true :
            data = db.GetStudietijdVakGBStudent(Vakid,starttime,stoptime);
                break;
        }

        PointPairList list1 = new PointPairList();
        for (int i = 0; i < data.Count; i++)
        {
            double x = i + 1;
            double y1 = data[i] / 60;
            list1.Add(x, y1);
        }

        LineItem myCurve = myPane.AddCurve("data",list1, Color.Red, SymbolType.Diamond);

        myPane.Title.FontSpec.FontColor = Color.Green;

        myPane.XAxis.MajorGrid.IsVisible = true;
        myPane.YAxis.MajorGrid.IsVisible = true;
        myPane.XAxis.MajorGrid.Color = Color.LightGray;
        myPane.YAxis.MajorGrid.Color = Color.LightGray;

        myPane.Legend.IsVisible = false;

        myCurve.Line.Width = 2.0F;

        myCurve.Line.Fill = new Fill(Color.White, Color.Red, 45F);

        myCurve.Symbol.Size = 8.0F;
        myCurve.Symbol.Fill = new Fill(Color.White);

        myPane.Chart.Fill = new Fill(Color.White,
           Color.FromArgb(255, 255, 210), -45F);
        masterPane.AxisChange();
    }


    public void MentorOverzicht(ZedGraphWeb webObject, Graphics g, MasterPane masterPane, String Titel, String Xas, String Yas, String Mentor,
        TextBox kleuren, TextBox vaktxt)
    {
        GraphPane myPane = masterPane[0];
        Random rnd = new Random();
        kleuren.Text ="";
        vaktxt.Text ="";

        myPane.Title.Text = Titel;
        myPane.XAxis.Title.Text = Xas;
        myPane.YAxis.Title.Text = Yas;


        List<String> users = new List<String>();
        users = db.GetMentorGebruikers(Gebruiker);
        String richting = db.GetMentorRichting(Gebruiker);

        List<Vak> vakken = new List<Vak>();
        vakken = db.GetVakkenRichting(int.Parse(richting));
        double tijd = 0;

        PointPairList list = new PointPairList();

        for (int i = 0; i < users.Count; i++)
        {
            list.Clear();
            for (int t = 0; t < vakken.Count; t++)
            {
                tijd = db.GetStudieTijdPerStudentVoorVak(vakken[t].Id.ToString(), users[i]);
                list.Add((double)t + 1, tijd );
            }
            int rood = rnd.Next(255);
            int groen = rnd.Next(255);
            int blauw = rnd.Next(255);
            BarItem myCurve = myPane.AddBar("K" + ((int)(i+1)).ToString(), list, Color.FromArgb(rood, groen, blauw));
            kleuren.Text += "\nK" + ((int)(i + 1)).ToString() + " " + users[i];
        }

       myPane.Chart.Fill = new Fill(Color.White,
          Color.FromArgb(255, 255, 166), 45.0F);

       masterPane.AxisChange();
       myPane.YAxis.Scale.Max += myPane.YAxis.Scale.MajorStep;

       BarItem.CreateBarLabels(myPane, false, "f0");

        for (int i = 0; i < vakken.Count; i++)
			{
                vaktxt.Text += "\n" + ((int)(i + 1)).ToString() + " " + vakken[i].Naam;
			}

    }
    

}
